package com.wwwarehouse.carddesk.adapter.teamadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.teambean.AllPowerDetailBean;

import java.util.List;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/14 9:16
 * @描述 ${TODO}
 */

public class AllPowerDetailAdapter extends BaseAdapter {

    private Context                  mContext;
    private List<AllPowerDetailBean.ListBean> mList;
    private ListView                 mListView;

    public AllPowerDetailAdapter(Context context, List list, ListView listView) {
        mContext = context;
        mList = list;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AllPowerDetailBean.ListBean listBean = mList.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.all_power_detail_item, null);
            viewHolder = new ViewHolder();
            viewHolder.tvPower = (TextView) convertView.findViewById(R.id.tv_power);
            viewHolder.tvCompanyName = (TextView) convertView.findViewById(R.id.tv_company_name);
            viewHolder.tvTime = (TextView) convertView.findViewById(R.id.tv_time);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                int itemHeight = (mListView.getMeasuredHeight() - mListView.getPaddingTop() - mListView.getPaddingBottom()) / 7;
                params.height = itemHeight;
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        viewHolder.tvPower.setText(String.format(mContext.getResources().getString(R.string.teamdesk_allpower_name), listBean.getTagName(), listBean.getItemName()));
        viewHolder.tvCompanyName.setText(listBean.getBusinessName());
        viewHolder.tvTime.setText(listBean.getValidDate()+mContext.getResources().getString(R.string.teamdesk_time_time)+listBean.getInvalidDate());
        return convertView;
    }

    private class ViewHolder{
        private TextView tvPower;
        private TextView tvCompanyName;
        private TextView tvTime;
    }
}
